<template>
	<div class="">
		<div class="crumbs">
			<h2 class="">讲师科目管理</h2>
		</div>
		<div class="container ">
			<div class="handle-box margin-bottom20 row-me row-center">
				<el-form :model="query" label-width="80px" class="ipt row-me">
					<el-form-item label="模糊搜索">
						<el-input v-model="query.name" placeholder="请输入姓名、手机号"></el-input>
					</el-form-item>
				</el-form>

				<div class="margin-bottom20 margin-left20">
					<el-button type="primary" @click="handleSearch">查询</el-button>
					<el-button @click="resetClick">重置</el-button>
					<el-button type="primary" class="handle-del" @click="exportClick">批量导出
					</el-button>
					<el-button type="primary" class="handle-del" @click="addClick">新增
					</el-button>
				</div>
			</div>
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="40" align="center"></el-table-column>
				<el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
				<el-table-column prop="name" label="讲师科目名称" align="center"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
				<el-table-column label="操作" width="180" align="center">
					<template slot-scope="scope">
						<el-button type="text" @click="handleEdit(scope.$index, scope.row),(dialogTitle = '编辑')">编辑
						</el-button>
						<el-popconfirm title="确定删除？" @confirm="clickDel(scope.row.id)">
							<el-button type="text" slot="reference">删除</el-button>
						</el-popconfirm>

					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageNumber"
					:page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 科目 -->
		<el-dialog :title="addTit" :visible.sync="courseShow" v-if="courseShow" width="40%">

			<el-form :model="courseForm" :rules="rules" ref="courseForm" label-width="120px"
				class="demo-addClassesForm">

				<el-form-item label="讲师科目名称" prop="name">
					<el-input v-model="courseForm.name" placeholder="请输入"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="courseConfirm('courseForm')">确定</el-button>
					<el-button @click="courseShow = false">取消</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		name: 'categoryTable',
		data() {
			return {
				query: {
					name: '',
					type: '',
					pageNumber: 1,
					pageSize: 30
				},
				dialogTitle: '',
				tableData: [],
				multipleSelection: [],
				editVisible: false,
				pageTotal: 0,
				form: {},
				areaList: [{
					value: 'zhinan',
					label: '指南',
					children: [{
						value: 'shejiyuanze',
						label: '设计原则',
						children: [{
							value: 'yizhi',
							label: '一致'
						}, {
							value: 'fankui',
							label: '反馈'
						}, {
							value: 'xiaolv',
							label: '效率'
						}, {
							value: 'kekong',
							label: '可控'
						}]
					}, {
						value: 'daohang',
						label: '导航',
						children: [{
							value: 'cexiangdaohang',
							label: '侧向导航'
						}, {
							value: 'dingbudaohang',
							label: '顶部导航'
						}]
					}]
				}], //地区
				approveList: [],
				addTit: '新增',
				courseShow: false,
				courseForm: {},
				rules: {
					name: [{
						required: true,
						message: '请输入',
						trigger: 'blur'
					}],
				},
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 讲师科目名称
			courseConfirm(courseForm) {
				this.$refs[courseForm].validate((valid) => {
					if (valid) {
						var url;
						if (this.$.isEmpty(this.courseForm.id)) {
							url = '/admin/minis/labelTypeSave';
						} else {
							url = '/admin/minis/labelTypeUpdate';
						}
						this.$post(url, this.courseForm).then((res) => {
							if (res.code == 1000) {
								this.$message.success('操作成功');
								this.courseShow = false;
								this.getData();
							}
						}).catch((error) => {
							console.log(error)
						})

					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			addClick() {
				this.addTit = '新增';
				this.courseForm = {};
				this.courseShow = true;
			},
			parseParams(data) {
				try {
					var tempArr = [];
					for (var i in data) {
						var key = encodeURIComponent(i);
						var value = encodeURIComponent(data[i]);
						tempArr.push(key + '=' + value);
					}
					var urlParamsStr = tempArr.join('&');
					return urlParamsStr;
				} catch (err) {
					return '';
				}
			},
			// 批量导出
			exportClick() {
				if (this.multipleSelection.length == 0) {
					this.$message.warning('请先选择数据!')
					return
				}
				var list = [];
				this.multipleSelection.forEach((item, index) => {
					list.push(item.id)
				});

				let params = {};
				// params.pageSize = 1000000000;
				// let appid = localStorage.getItem('htk_appid');
				params.token = localStorage.getItem('token');
				params.ids = list;
				window.open(this.$host + '/admin/minis/labelTypeExport?' + this.parseParams(params));
			},
			// 选择省区
			handleChange(value) {
				console.log(value);
			},
			// 获取 easy-mock 的模拟数据
			getData() {
				this.$postForm('/admin/minis/teacherAll', this.query).then((res) => {
					if (res.code == 1000) {
						this.$forceUpdate(); //强制刷新
						this.tableData = res.data.list
						this.pageTotal = res.data.totalRow
					}
				}).catch((error) => {
					console.log(error)
				})

			},
			// 触发搜索按钮
			handleSearch() {
				this.$set(this.query, 'pageNumber', 1);
				this.getData();
			},
			// 点击重置
			resetClick() {
				this.query = {};
				this.$set(this.query, 'pageNumber', 1);
				this.getData();
			},
			// 多选操作
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			// 编辑操作
			handleEdit(index, row) {
				this.title = '编辑';
				this.idx = index;
				this.courseForm = JSON.parse(JSON.stringify(row)) // 深拷贝
				this.courseShow = true;
			},
			clickDel(id) {
				this.$postForm('/admin/minis/labelTypeDelete', {
					id: id,
				}).then((res) => {
					if (res.code == 1000) {
						this.$message.success('删除成功');
						this.getData();
					}
				}).catch((error) => {
					console.log(error)
				})
			},

			// 分页导航
			handlePageChange(val) {
				this.$set(this.query, 'pageNumber', val);
				this.getData();
			}
		}
	};
</script>

<style>
</style>